import { DirectorServiceImpl } from "shared/modules/director/service/DirectorServiceImpl";

export default class DirectorServerServiceImpl extends DirectorServiceImpl {
    /** 玩家信息表 */
    playerMap = new Map<Player, Ctn.player.PlayerEntity>()
    addPlayer(playerEntity: Ctn.player.PlayerEntity) {
        this.playerMap.set(playerEntity.player, playerEntity);
        this.addPlayerEventBinder.callBack(playerEntity)
    }
    public run(): void {
        const Players = game.GetService('Players');
        Players.CharacterAutoLoads = false;
        Players.GetPlayers().forEach(player => this.addPlayer({ player }))
        Players.PlayerAdded.Connect(player => this.addPlayer({ player }))
        Players.PlayerRemoving.Connect(player => {
            const playerEntity = this.playerMap.get(player);
            if (!playerEntity) return
            this.removePlayerEventBinder.callBack(playerEntity);
            this.playerMap.delete(player);

        })
    }
    syncPlayerEntity(entity: Ctn.player.PlayerEntity) {
        this.fire('syncPlayerEntity', entity, { player: entity.player })
    }
    public init(): void {
        /* 清空设计文件夹 */
        const designfolder = game.Workspace.FindFirstChild('designer');
        designfolder?.GetChildren().forEach(c => {
            if (c.Name !== 'box') c.Destroy();
        })
    }
}
